package com.hw.one.collect.heartbeat2;

import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.net.Socket;

/**
 * @auther : 王振
 * @createtime:2018/10/25 9:58
 */
public class ClientSender {
    private ClientSender() {
    }

    Socket sender = null;
    private static ClientSender instance;

    public static ClientSender getInstance() {
        if (instance == null) {
            synchronized (ClientHeart.class) {
                instance = new ClientSender();
            }
        }
        return instance;
    }

    public void send() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_mm_dd HH:mm:ss");
        try {
            sender = new Socket(InetAddress.getLocalHost(), 65255);
            while (true) {
                ObjectOutputStream out = new ObjectOutputStream(sender.getOutputStream());
                HeartbeatEntity obj = new HeartbeatEntity();
                obj.setTime(simpleDateFormat.format(System.currentTimeMillis()));//获取系统的的当前时间（毫秒）
                obj.setNodeID("id");
                obj.setError("error");
                obj.setInfo(new HashMap<String, Object>());
                out.writeObject(obj);
                out.flush();

                System.out.println("已发送...");
                Thread.sleep(5000);
            }
        } catch (Exception e) {

        }
    }

}
